﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Application;
using Framework;

namespace UserControls
{
    public partial class DropDownListImage : System.Web.UI.UserControl
    {
        public List<VOImage> images;
        private PagedDataSource pdsImages;

        private int CurrentFrame
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            images = BusinessImage.GetAllImages();
            int nbExtraButtons = GetExtraButtonsNumber(images.Count);
            for (int i = 0; i < nbExtraButtons; i++)
            {
                LinkButton buttNumber = new LinkButton();
                buttNumber.Text = (i + 1).ToString();
                buttNumber.Command += new CommandEventHandler(lnkInBetween_Click);
                buttNumber.CommandArgument = (i).ToString();
                buttNumber.ID = "lnkBtn" + buttNumber.CommandArgument;
                AddAsyncPostBackTrigger(buttNumber.ID);
                Panel pnlLinkButton = new Panel();
                pnlLinkButton.Controls.Add(buttNumber);
                pnlInBetween.Controls.Add(pnlLinkButton);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CurrentFrame = 0;
                populateRepeater();
            }
        }

        private void populateRepeater()
        {
            pdsImages = new PagedDataSource();
            pdsImages.AllowPaging = true;
            pdsImages.CurrentPageIndex = CurrentFrame;
            AddSelectedClass();
            pdsImages.PageSize = 3;
            pdsImages.DataSource = images;
            if (pdsImages.IsFirstPage)
            {
                btnPrevious.Enabled = false;
                pnlPrevious.CssClass += " selected";
                btnPrevious.ImageUrl = "/Images/Test/previous_left_white.png";
            }
            else {
                btnPrevious.Enabled = true;
                pnlPrevious.CssClass = "previous-butt";
                btnPrevious.ImageUrl = "/Images/Test/previous_left.png";
            }
            if (pdsImages.IsLastPage)
            {
                btnNext.Enabled = false;
                pnlNext.CssClass += " selected";
                btnNext.ImageUrl = "/Images/Test/next_right_white.png";
            }
            else
            {
                btnNext.Enabled = true;
                pnlNext.CssClass = "next-butt";
                btnNext.ImageUrl = "/Images/Test/next_right.png";
            }

            rpterImgLine.DataSource = pdsImages;
            rpterImgLine.DataBind();
        }

        private void AddSelectedClass()
        {
            int nbFrame = pnlInBetween.Controls.Count - 1;
            for (int i = 0; i < nbFrame; i++)
            {
                Panel selectedPnl = ((Panel)(pnlInBetween.Controls[i + 1]));
                LinkButton selectedButt = ((LinkButton)(selectedPnl.Controls[0]));
                if (i == CurrentFrame)
                {
                    selectedPnl.CssClass += "selected";
                    selectedButt.Enabled = false;
                }
                else
                {
                    selectedPnl.CssClass = "";
                    selectedButt.Enabled = true;
                }
            }
        }
        private int GetExtraButtonsNumber(int itemCount)
        {
            int nbExtraButtons = 0;
            if (itemCount % 3 == 0)
            {
                nbExtraButtons = itemCount / 3;
            }
            else
            {
                nbExtraButtons = (itemCount / 3) + 1;
            }
            return nbExtraButtons;
        }

        private void AddAsyncPostBackTrigger(string id)
        {
            AsyncPostBackTrigger asyncPostBackTrigger = new AsyncPostBackTrigger();
            asyncPostBackTrigger.EventName = "Click";
            asyncPostBackTrigger.ControlID = id;
            updatepanelPagingDDLImages.Triggers.Add(asyncPostBackTrigger);
        }

        protected void lnkFirst_Click(object sender, EventArgs e)
        {
            CurrentFrame = 0;
            populateRepeater();
        }

        protected void lnkPrevious_Click(object sender, EventArgs e)
        {
            --CurrentFrame;
            populateRepeater();
        }

        protected void lnkNext_Click(object sender, EventArgs e)
        {
            ++CurrentFrame;
            populateRepeater();
        }

        protected void lnkInBetween_Click(object sender, CommandEventArgs e)
        {
            CurrentFrame = MappingTool.CatchIntNull(e.CommandArgument);
            populateRepeater();
        }
        protected void lnkLast_Click(object sender, EventArgs e)
        {
            CurrentFrame = pdsImages.PageCount - 1;
            populateRepeater();
        }
        protected void select_click(object sender, EventArgs e)
        {
            imgSelectedImage.ImageUrl = "/Images/Templates/Night.jpg";
            updatePanelAddImg.Update();
        }
    }
}